package com.example.spinning.controller;

import com.example.spinning.bean.*;
import com.example.spinning.service.DeliverOrderService;
import com.example.spinning.service.EndProductStockService;
import com.example.spinning.service.InputStockService;
import com.example.spinning.service.OutStockService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventoryManagement")
public class InventoryManagementController {

    @Autowired
    private EndProductStockService endProductStockService;

    @Autowired
    private DeliverOrderService deliverOrderService;

    @Autowired
    private InputStockService inputStockService;

    @Autowired
    private OutStockService outStockService;

    private Logger logger = LoggerFactory.getLogger(getClass());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //得到所有的产品库存
    @RequestMapping(path = "/allProduct",method = RequestMethod.GET)
    public Map<String,Object> getAllEndProductStocks(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = endProductStockService.getEndProductCount();
        List<endProductStock> productStocks = endProductStockService.getAllEndProductStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("productStocks",productStocks);
        return map;
    }

    //得到所有出货单信息
    @RequestMapping(path = "/allDeliverOrder",method = RequestMethod.GET)
    public Map<String, Object> getDeliverOrder(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = deliverOrderService.getDeliverOrderCount();
        List<DeliverOrder> orders = deliverOrderService.getAllDeliverOrder(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("orders",orders);
        return map;
    }


    //得到所有入库单信息
    @RequestMapping(path = "/allInputStock",method = RequestMethod.GET)
    public Map<String, Object> getAllInputOrder(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = inputStockService.getInputStockCount();
        List<InputStock> inputStocks = inputStockService.getAllInputStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("inputStocks",inputStocks);
        return map;
    }

    //得到所有的出库单信息
    @RequestMapping(path = "/allOutStock",method = RequestMethod.GET)
    public Map<String, Object> getAllOutStock(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = outStockService.getOutStockCount();
        List<OutStock> outStocks = outStockService.getAllOutStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("outStocks",outStocks);
        return map;
    }


    //通过id查找库存
    @RequestMapping(path = "/get/{id}",method = RequestMethod.GET)
    public endProductStock getEndProductStockById(@PathVariable(value = "id") Long id){
        return endProductStockService.getEndProductStockById(id);
    }

    //通过id查找出货单
    @RequestMapping(path = "/getDeliverOrder/{id}",method = RequestMethod.GET)
    public DeliverOrder getDeliverOrderByIds(@PathVariable Long id){
        return deliverOrderService.getDeliverOrderById(id);
    }

    //通过id查找入库单
    @RequestMapping(path = "/getInputStock/{id}",method = RequestMethod.GET)
    public InputStock getInputStockByIds(@PathVariable("id") Long id){
        return inputStockService.getInputStockById(id);
    }

    //通过id查找出库单
    @RequestMapping(path = "/getOutStock/{id}",method = RequestMethod.GET)
    public OutStock getOutStockByIds(@PathVariable("id") Long id){
        return outStockService.getOutStockById(id);
    }

    //通过条件模糊查找库存
    @RequestMapping(path = "/findEndByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getEndProductStockByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
             String name,
            String batch_no,
            String norms,
            String date
    ){
        int totalCount = endProductStockService.getEndProductCountByCondition(name,batch_no,norms,date);
        List<endProductStock> productStocks = endProductStockService.getEndProduceStockByCondition(page,count,name,batch_no,norms,date);
        Map<String,Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("productStocks",productStocks);
        return map;
    }

    //根据条件模糊查找出货单
    @RequestMapping(path = "/findDeliverByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getDeliverOrderByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String date,
            String receiver,
            String norms,
            String batch_no
    ){
        int totalCount = deliverOrderService.getDeliverOrderCountByCondition(date,receiver,norms,batch_no);
        List<DeliverOrder> deliverOrders = deliverOrderService.getDeliverOrderByCondition(page,count,date,receiver,norms,batch_no);
        Map<String,Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("deliverOrders",deliverOrders);
        return map;
    }

    //根据条件模糊查找入库单
    @RequestMapping(path = "/findInputByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getInputStockByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String unit,
            String date,
            String name,
            String function
    ){
        int totalCount = inputStockService.getInputStockCountByCondition(unit,date,name,function);
        List<InputStock> inputStocks = inputStockService.getInputStockByCondition(page,count,unit,date,name,function);
        Map<String,Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("inputStocks",inputStocks);
        return map;
    }

    //根据条件模糊查找出库单
    @RequestMapping(path = "/findOutByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getOutStockByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String date,
            String receiver,
            String norms,
            String batch_no
    ){
        int totalCount = outStockService.getOutStockCountByCondition(date,receiver,norms,batch_no);
        List<OutStock> outStocks = outStockService.getOutStockByCondition(page,count,date,receiver,norms,batch_no);
        Map<String,Object> map = new HashMap<>();
        map.put("totleCount",totalCount);
        map.put("outStocks",outStocks);
        return map;
    }

    //添加产品库存
    @RequestMapping(path = "/addEndProduct/",method = RequestMethod.POST)
    public RespBean addEndProduct(endProductStock productStock) {
        if ("".equals(productStock.getName())|| productStock.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(productStock.getBatch_no())|| productStock.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = endProductStockService.addEndProductStock(productStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加出货单信息
    @RequestMapping(path = "/addDeliverOrder/",method = RequestMethod.POST)
    public RespBean addDeliverOrder(DeliverOrder deliverOrder) {

        int result = deliverOrderService.addDeliverOrder(deliverOrder);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加入库单信息
    @RequestMapping(path = "/addInputStock/",method = RequestMethod.POST)
    public RespBean addInputStock(InputStock inputStock) {

        int result = inputStockService.addInputStock(inputStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加出库单信息
    @RequestMapping(path = "/addOutStock/",method = RequestMethod.POST)
    public RespBean addOutStock(OutStock outStock) {

        int result = outStockService.addOutStock(outStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //修改产品库存
    @RequestMapping(path = "/updateEnd/",method = RequestMethod.PUT)
    public RespBean updateEndProStock(endProductStock productStock){
        if ("".equals(productStock.getName())|| productStock.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(productStock.getBatch_no())|| productStock.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        //修改行数
        int i = endProductStockService.updateEndProductStockById(productStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return  new RespBean("error","修改失败！");
    }

    //修改出货单信息
    @RequestMapping(path = "/updateDeliver/",method = RequestMethod.PUT)
    public RespBean updateDeliverOrder(DeliverOrder deliverOrder){
        int i = deliverOrderService.updateDeliverOrder(deliverOrder);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改入库单信息
    @RequestMapping(path = "/updateInputStock/",method = RequestMethod.PUT)
    public RespBean updateInputStock(InputStock inputStock){
        int i = inputStockService.updateInputStock(inputStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改出库单信息
    @RequestMapping(path = "/updateOutStock/",method = RequestMethod.PUT)
    public RespBean updateOutStock(OutStock outStock){
        int i = outStockService.updateOutStock(outStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //删除库存产品
    @RequestMapping(value = "/del/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteEndProductStockById(@PathVariable("ids") String ids){
        boolean result = endProductStockService.deleteEndProductStockByIds(ids);
        if(result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除出货单
    @RequestMapping(path = "/delDeliver/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteDeliverById(@PathVariable("ids") String ids){
        boolean result = deliverOrderService.deleteDeliverOrderByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除入库单
    @RequestMapping(path = "/delInputStock/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteInputStockById(@PathVariable("ids") String ids){
        boolean result = inputStockService.deleteInputStockByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除出库单
    @RequestMapping(path = "/delOutStock/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteOutStockById(@PathVariable("ids") String ids){
        boolean result = outStockService.deleteOutStockByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

}
